#include <memory>
#include <thread>
#include <vector>

#include "client/rpc_client.h"
#include "monitor/cpu_load_monitor.h"
#include "monitor/cpu_softirq_monitor.h"
#include "monitor/cpu_stat_monitor.h"
#include "monitor/mem_monitor.h"
#include "monitor/monitor_inter.h"
#include "monitor/net_monitor.h"

#include "monitor_info.grpc.pb.h"
#include "monitor_info.pb.h"

int main()
{
    std::vector<std::shared_ptr<monitor::MonitorInter>> runners_;
    // 创建各个指标的工厂类对象
    monitor::MonitorFactory* cpu_soft_irq_factory = new monitor::CpuSoftIrqFactory();
    monitor::MonitorFactory* cpu_load_factory = new monitor::CpuLoadFactory();
    monitor::MonitorFactory* cpu_stat_factory = new monitor::CpuStatFactory();
    monitor::MonitorFactory* mem_factory = new monitor::MemFactory();
    monitor::MonitorFactory* net_factory = new monitor::NetFactory();
    // 指向子类的父类指针
    runners_.emplace_back(cpu_soft_irq_factory->CreateMonitor());
    runners_.emplace_back(cpu_load_factory->CreateMonitor());
    runners_.emplace_back(cpu_stat_factory->CreateMonitor());
    runners_.emplace_back(mem_factory->CreateMonitor());
    runners_.emplace_back(net_factory->CreateMonitor());

    // 提供grpc服务的ip地址
    std::string server_address = "localhost:50051";
    monitor::RpcClient rpc_client_(server_address);
    // 获取当前用户名，用于在分布式监控中区分机器
    char *name = getenv("USER");
    std::unique_ptr<std::thread> thread_(new std::thread([&]()
                                                         { 
        while (true) 
        {
            monitor::proto::MonitorInfo monitor_info;
            monitor_info.set_name(std::string(name));
            // 遍历，把MonitorInfo的每个对象都填满，完成一次监控
            for (auto& runner : runners_) {
            runner->UpdateOnce(&monitor_info);
        }

        rpc_client_.SetMonitorInfo(monitor_info);
        std::this_thread::sleep_for(std::chrono::seconds(3));
    } }));

    thread_->join();
    return 0;
}
